package com.moody.hibernate.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.CustAddress;
import com.moody.hibernate.domain.CustEvents;

/**
 * A data access object (DAO) providing persistence and search support for
 * CustEvents entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.moody.hibernate.domain.CustEvents
 * @author MyEclipse Persistence Tools
 */

public class CustEventsDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(CustEventsDAO.class);
	// property constants
	public static final String CUST_ID = "custId";
	public static final String EVENT_TYPE = "eventType";
	public static final String EVENT_DESC = "eventDesc";
	public static final String INPUT_USER_ID = "inputUserId";
	public static final String INPUT_ORG_ID = "inputOrgId";
	public static final String INPUT_DATE = "inputDate";
	public static final String UPDATE_USER_ID = "updateUserId";
	public static final String UPDATE_ORG_ID = "updateOrgId";
	public static final String UPDATE_DATE = "updateDate";

	protected void initDao() {
		// do nothing
	}

	public void save(CustEvents transientInstance) {
		log.debug("saving CustEvents instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void update(CustEvents transientInstance) {
		log.debug("updating CustEvents instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public void delete(CustEvents persistentInstance) {
		log.debug("deleting CustEvents instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CustEvents findById(java.lang.String id) {
		log.debug("getting CustEvents instance with id: " + id);
		try {
			CustEvents instance = (CustEvents) getHibernateTemplate().get(
					"com.moody.hibernate.domain.CustEvents", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(CustEvents instance) {
		log.debug("finding CustEvents instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CustEvents instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CustEvents as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCustId(Object custId) {
		return findByProperty(CUST_ID, custId);
	}

	public List findByEventType(Object eventType) {
		return findByProperty(EVENT_TYPE, eventType);
	}

	public List findByEventDesc(Object eventDesc) {
		return findByProperty(EVENT_DESC, eventDesc);
	}

	public List findByInputUserId(Object inputUserId) {
		return findByProperty(INPUT_USER_ID, inputUserId);
	}

	public List findByInputOrgId(Object inputOrgId) {
		return findByProperty(INPUT_ORG_ID, inputOrgId);
	}

	public List findByInputDate(Object inputDate) {
		return findByProperty(INPUT_DATE, inputDate);
	}

	public List findByUpdateUserId(Object updateUserId) {
		return findByProperty(UPDATE_USER_ID, updateUserId);
	}

	public List findByUpdateOrgId(Object updateOrgId) {
		return findByProperty(UPDATE_ORG_ID, updateOrgId);
	}

	public List findByUpdateDate(Object updateDate) {
		return findByProperty(UPDATE_DATE, updateDate);
	}

	public List findAll() {
		log.debug("finding all CustEvents instances");
		try {
			String queryString = "from CustEvents";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CustEvents merge(CustEvents detachedInstance) {
		log.debug("merging CustEvents instance");
		try {
			CustEvents result = (CustEvents) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CustEvents instance) {
		log.debug("attaching dirty CustEvents instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CustEvents instance) {
		log.debug("attaching clean CustEvents instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CustEventsDAO getFromApplicationContext(ApplicationContext ctx) {
		return (CustEventsDAO) ctx.getBean("CustEventsDAO");
	}
	
	public void deleteById(String serialNo){
		String hql = "delete from CustEvents where serialNo = ?";
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createQuery(hql);query.setParameter(0, serialNo);
		if(query.executeUpdate() > 0)
			log.info("删除成功");
		else
			log.info("删除0个元素");
		session.close();
		
	}
	
	
}